﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MainClient
{
    public partial class frmTableManager : Form
    {
        public frmTableManager()
        {
            InitializeComponent();
        }

        private void frmTableManager_Load(object sender, EventArgs e)
        {
            try
            {
                // Clear all tables
                while (this.dgv_tables.Rows.Count > 0)
                {
                    this.dgv_tables.Rows.RemoveAt(0);
                }

                DataAccess.TableDAO tableDAO = new DataAccess.TableDAO();
                DataSet ds = tableDAO.getTables(DataAccess.TableDAO.TYPE_ALL,
                                DataAccess.TableDAO.STATE_ALL);
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    int index = this.dgv_tables.Rows.Add();
                    DataGridViewRow newrow = this.dgv_tables.Rows[index];
                    newrow.Cells[0].Value = dr["tableName"];
                    newrow.Cells[1].Value = DataAccess.TableDAO.getTableTypeString(
                                                                        Int32.Parse(dr["tableType"].ToString()));
                    newrow.Cells[2].Value = DataAccess.TableDAO.getTableStateString(
                                                                        Int32.Parse(dr["tableState"].ToString()));
                    newrow.Cells[3].Value = DataAccess.TableDAO.getStringMoney(
                                                                        double.Parse(dr["tablePrice"].ToString()));
                    newrow.Cells[4].Value = dr["notes"];
                }

                //this.dgv_tables.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception e1)
            {
                Console.WriteLine(e1);
                String strError = "加载桌位信息失败！";
                MessageBox.Show(strError, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_addTable_Click(object sender, EventArgs e)
        {
            DataGridViewRow selectedRow = this.dgv_tables.SelectedRows[0];
            string strTableName = selectedRow.Cells[0].Value.ToString().Trim();
            frmTableInfo frmD = new frmTableInfo(strTableName, frmTableInfo.OPER_ADDTABLE);
            if (frmD.ShowDialog() == DialogResult.OK)
            {
                this.frmTableManager_Load(sender, e);
            }
        } // end of function

        private void btn_updTable_Click(object sender, EventArgs e)
        {
            DataGridViewRow selectedRow = this.dgv_tables.SelectedRows[0];
            string strTableName = selectedRow.Cells[0].Value.ToString().Trim();
            frmTableInfo frmD = new frmTableInfo(strTableName, frmTableInfo.OPER_UPDTABLE);
            if (frmD.ShowDialog() == DialogResult.OK)
            {
                this.frmTableManager_Load(sender, e);
            }
        } // end of function

        private void btm_rmTable_Click(object sender, EventArgs e)
        {
            DataGridViewRow selectedRow = this.dgv_tables.SelectedRows[0];
            string strTableName = selectedRow.Cells[0].Value.ToString().Trim();
            frmTableInfo frmD = new frmTableInfo(strTableName, frmTableInfo.OPER_RMTABLE);
            if (frmD.ShowDialog() == DialogResult.OK)
            {
                this.frmTableManager_Load(sender, e);
            }
        } // end of function


    }
}
